package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StreamUtils;
import com.joker.common.utils.StringUtils;
import com.joker.orange.domain.NovelInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.NovelChapterBo;
import com.joker.orange.domain.vo.NovelChapterVo;
import com.joker.orange.domain.NovelChapter;
import com.joker.orange.mapper.NovelChapterMapper;
import com.joker.orange.service.INovelChapterService;

import java.math.BigDecimal;
import java.util.*;

/**
 * 小说章节Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class NovelChapterServiceImpl implements INovelChapterService {

    private final NovelChapterMapper baseMapper;

    /**
     * 查询上一章节
     */
    @Override
    public NovelChapterVo selectLastChapterById(NovelChapterBo bo) {
        return baseMapper.selectLastChapterById(bo);
    }

    /**
     * 查询下一章节
     */
    @Override
    public NovelChapterVo selectNextChapterById(NovelChapterBo bo) {
        return baseMapper.selectNextChapterById(bo);
    }

    /**
     * 查询小说章节
     */
    @Override
    public NovelChapterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询小说章节列表
     */
    @Override
    public TableDataInfo<NovelChapterVo> queryPageList(NovelChapterBo bo, PageQuery pageQuery) {
        QueryWrapper<NovelChapter> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(bo.getNovelId()), "c.novel_id", bo.getNovelId())
            .like(StringUtils.isNotBlank(bo.getChapterTitle()), "c.chapter_title", bo.getChapterTitle())
            .eq(StringUtils.isNotBlank(bo.getChargeType()), "c.charge_type", bo.getChargeType());
        Page<NovelChapterVo> result = baseMapper.queryPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    /**
     * 查询小说章节列表
     */
    @Override
    public List<NovelChapterVo> queryList(NovelChapterBo bo) {
        LambdaQueryWrapper<NovelChapter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<NovelChapterVo> selectNovelChapterByNovelId(Long novelId) {
        QueryWrapper<NovelChapter> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(novelId), "c.novel_id", novelId);
        return baseMapper.queryPageList(wrapper);
    }

    private LambdaQueryWrapper<NovelChapter> buildQueryWrapper(NovelChapterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NovelChapter> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getNovelId() != null, NovelChapter::getNovelId, bo.getNovelId());
        lqw.eq(StringUtils.isNotBlank(bo.getChapterContent()), NovelChapter::getChapterContent, bo.getChapterContent());
        lqw.eq(StringUtils.isNotBlank(bo.getChapterTitle()), NovelChapter::getChapterTitle, bo.getChapterTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeType()), NovelChapter::getChargeType, bo.getChargeType());
        lqw.eq(bo.getConsumeDiamond() != null, NovelChapter::getConsumeDiamond, bo.getConsumeDiamond());
        return lqw;
    }

    /**
     * 新增小说章节
     */
    @Override
    public Boolean insertByBo(NovelChapterBo bo) {
        NovelChapter add = BeanUtil.toBean(bo, NovelChapter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertBatch(List<NovelChapter> chapterList) {
        return baseMapper.insertBatch(chapterList);
    }

    /**
     * 修改小说章节
     */
    @Override
    public Boolean updateByBo(NovelChapterBo bo) {
        NovelChapter update = BeanUtil.toBean(bo, NovelChapter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NovelChapter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除小说章节
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void updateBatchChapter(String chargeType, String consumeDiamond, Long[] chapterIds) {
        if (ArrayUtil.isNotEmpty(chapterIds)) {
            // 批量修改
            List<NovelChapter> list = StreamUtils.toList(Arrays.asList(chapterIds), chapterId -> {
                NovelChapter novelChapter = new NovelChapter();
                novelChapter.setId(chapterId);
                novelChapter.setChargeType(chargeType);
                if ("2".equals(chargeType)) {
                    novelChapter.setConsumeDiamond(new BigDecimal(consumeDiamond));
                }
                return novelChapter;
            });
            baseMapper.updateBatchById(list);
        }
    }
}
